home *** CD-ROM | disk | FTP | other *** search
/ Scene 96 / Scene 96 International Edition (Zyklop Software) (Disc 1) (1997).iso / emulator / amiga065 / amiga / source / transdis.c next >
Encoding:
C/C++ Source or Header  |  1996-10-31  |  3.8 KB  |  161 lines

  1. /*
  2.  * Transdisk V4.1
  3.  * Copyright 1995,1996 Bernd Schmidt, Marcus Sundberg, Stefan Ropke,
  4.  *                     Rodney Hester
  5.  *
  6.  * Use DICE and 2.0 includes or above to compile
  7.  */
  8.  
  9. #include <stdio.h>
  10.  
  11. #include <exec/devices.h>
  12. #include <exec/io.h>
  13. #include <exec/memory.h>
  14. #include <devices/trackdisk.h>
  15.  
  16. #include <clib/alib_protos.h>
  17. #include <clib/exec_protos.h>
  18.  
  19. static void usage()
  20. {
  21.     fprintf(stderr, "Usage: transdisk options\n");
  22.     fprintf(stderr, "Recognized options:\n");
  23.     fprintf(stderr, "-h: Assume device is high density\n");
  24.     fprintf(stderr, "-d device unit: Use this device instead of DF0:\n");
  25.     fprintf(stderr, "-s n: Begin transfer at track n\n");
  26.     fprintf(stderr, "-e n: End transfer at track n\n");
  27.     fprintf(stderr, "-w filename: writes the ADF file <filename> to disk\n\n");
  28.     fprintf(stderr, "Example:\n");
  29.     fprintf(stderr, "transdisk >RAM:df1.adf.1 -d trackdisk 1 -s 0 -e 39\n");
  30.     fprintf(stderr, "transfers the first half of the floppy in DF1: into\n");
  31.     fprintf(stderr, "a file in the RAM disk.\n");
  32.     fprintf(stderr, "Or:\n");
  33.     fprintf(stderr, "transdisk -w test.adf\n");
  34.     fprintf(stderr, "writes the ADF-file test.adf to the disk in df0:\n");
  35. }
  36.  
  37. int main(int argc, char **argv)
  38. {
  39.     char *filename, *openMode="rb";
  40.     FILE *ADFFile;
  41.     int write=0;
  42.     struct IOStdReq *ioreq;
  43.     struct MsgPort *port;
  44.  
  45.     UBYTE *buffer;
  46.     char devicebuf[256];
  47.     char *devicename = "trackdisk.device";
  48.     char devicenum = 0;
  49.     int i;
  50.     int starttr = 0, endtr = 79;
  51.     int sectors = 11;
  52.  
  53.     for (i = 1; i < argc;) {
  54.     if (argv[i][0] != '-' || argv[i][2] != 0) {
  55.         usage();
  56.         exit(1);
  57.     }
  58.     switch (argv[i][1]) {
  59.      case 'h':
  60.         sectors = 22;
  61.         i++;
  62.         break;
  63.     case 'd':
  64.         if (i+2 >= argc) {
  65.         usage();
  66.         exit(1);
  67.         }
  68.         devicenum = atoi(argv[i+2]);
  69.         sprintf(devicebuf, "%s.device", argv[i+1]);
  70.         devicename = devicebuf;
  71.         i += 3;
  72.         break;
  73.     case 's':
  74.         if (i+1 >= argc) {
  75.         usage();
  76.         exit(1);
  77.         }
  78.         starttr = atoi(argv[i+1]);
  79.         i += 2;
  80.         break;
  81.     case 'e':
  82.         if (i+1 >= argc) {
  83.         usage();
  84.         exit(1);
  85.         }
  86.         endtr = atoi(argv[i+1]);
  87.         i += 2;
  88.         break;
  89.     case 'w':
  90.         if (i+1 >= argc) {
  91.         usage();
  92.         exit(1);
  93.         }
  94.         filename=argv[i+1];
  95.         write=1;
  96.         i += 2;
  97.         break;
  98.     default:
  99.         usage();
  100.         exit(1);
  101.     }
  102.     }
  103.     fprintf(stderr,"Using %s unit %d\n", devicename, devicenum);
  104.     fprintf(stderr,"Tracks are %d sectors\n", sectors);
  105.     fprintf(stderr,"First track %d, last track %d\n", starttr, endtr);
  106.  
  107.     buffer = AllocMem(512, MEMF_CHIP);
  108.     if (write) {
  109.     ADFFile = fopen(filename,openMode);
  110.  
  111.     if (!ADFFile) {
  112.         fprintf(stderr,"Error while opening input file\n");
  113.         exit (1);
  114.     }
  115.     }
  116.     
  117.     port = CreatePort(0, 0);
  118.     if (port) {
  119.     ioreq = CreateStdIO(port);
  120.     if (ioreq) {
  121.         if (OpenDevice(devicename, devicenum, ioreq, 0) == 0) {
  122.         int tr, sec;
  123.  
  124.         ioreq->io_Command = write ? CMD_WRITE : CMD_READ;
  125.         ioreq->io_Length = 512;
  126.         ioreq->io_Data = buffer;
  127.         for (tr = starttr*2; tr < (endtr+1)*2; tr++) {
  128.             fprintf(stderr,"Track: %d\r",tr/2);
  129.             for (sec = 0; sec < sectors; sec++) {
  130.             fflush(stderr);
  131.             if (write)
  132.                 if (fread(buffer, sizeof(UBYTE), 512, ADFFile) < 512) {
  133.                 fprintf(stderr, "Error: ADF file to short?\n");
  134.                 exit(1);
  135.                 }  
  136.             ioreq->io_Offset = 512 * (tr * sectors + sec);
  137.             DoIO(ioreq);
  138.             if (!write)
  139.                 fwrite(buffer, sizeof(UBYTE), 512, stdout);
  140.             }
  141.         }
  142.         if (write) {        /* Make sure the last track is written to disk */
  143.             ioreq->io_Command = CMD_UPDATE;
  144.             DoIO(ioreq);
  145.         }
  146.         ioreq->io_Command = TD_MOTOR;    /* Turn Disk-motor off */
  147.         ioreq->io_Length = 0;
  148.         DoIO(ioreq);
  149.         CloseDevice(ioreq);
  150.         } else
  151.         fprintf(stderr,"Unable to open %s unit %d\n", devicename, devicenum);
  152.         DeleteStdIO(ioreq);
  153.     }
  154.     }
  155.     fprintf(stderr,"\n");
  156.     FreeMem(buffer, 512);
  157.     if (write)
  158.     fclose (ADFFile);
  159.     return 0;
  160. }
  161.